<template>
  <div>
    <el-form
      ref="deptForm"
      :model="form"
      :rules="formRules"
      label-width="120px"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="form.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="form.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人">
        <el-select
          v-model="form.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in employees"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  getEmployeeSimple,
  addDepartments,
  updateDepartments
} from '@/api/departments'
export default {
  name: 'Deptdialog',
  // 子组件
  props: {
    curId: {
      type: String,
      required: true
    },
    isEdit: {
      type: [Boolean]
    },
    row: {
      type: [Object],
      default: () => {}
    }
  },
  data() {
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: '' // 父级部门的id
      },
      /* 表单 验证 */
      formRules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 500,
            message: '长度在 1 到 500 个字符',
            trigger: 'blur'
          }
        ]
      },
      employees: []
    }
  },
  watch: {
    isEdit: {
      handler(newVal) {
        if (newVal) {
          this.form = JSON.parse(JSON.stringify(this.row))
        }
      },
      immediate: true
    }
  },
  created() {
    this.loadEmployee()
  },
  methods: {
    // 获取员工简单列表
    async loadEmployee() {
      const res = await getEmployeeSimple()
      console.log(res.data)
      this.employees = res.data
    },
    // 点击确认
    async hSubmit() {
      const valid = await this.$refs.deptForm.validate().catch((e) => e)
      if (!valid) return
      // console.log(111)
      this.isEdit ? this.editBumen() : this.addBumen()
    },
    // 添加部门
    async addBumen() {
      // console.log(1111)
      this.form.pid = this.curId
      const res = await addDepartments(this.form)
      // console.log(res, 5)
      if (res.success) {
        /* 清空表单数据  */
        this.form = {}
        /* 子向父传值 */
        this.$emit('handleDia')
      }
    },
    // 编辑部门
    async editBumen() {
      // console.log(this.row, 789879)
      this.form.pid = this.row.pid
      this.form.id = this.row.id
      const res = await updateDepartments(this.form)
      // console.log(res, 5)
      if (res.success) {
        /* 清空表单数据  */
        this.form = {}
        /* 子向父传值 */
        this.$emit('handleDia')
      }
    },
    // 点击取消
    hCancel() {
      // 通知父组件去关闭弹层
      this.$emit('handleDia')
    }
  }
}
</script>

<style lang="scss" scoped></style>
